﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;


namespace ENT.PM.Core.Mail
{
    public static class Mail
    {
        

        /// <summary>
        /// Permite enviar un mail a traves de un servidor smtp
        /// </summary>
        /// <param name="strMailFrom">Desde</param>
        /// <param name="strMailTo">Hasta</param>
        /// <param name="strAsunto">Asunto</param>
        /// <param name="strMensajeHTML">Mensaje Html</param>
        /// <returns></returns>
        public static bool EnviarMail(string strMailFrom, string strMailTo, string strAsunto, string strMensajeHTML)
        {
            bool booEstadoMail = false;
            try
            {
             
                MailMessage mmCorreo = new MailMessage();
                MailAddressCollection to = new MailAddressCollection();
                
                if (strMailFrom=="")
                {
                    strMailFrom = ConfigurationManager.AppSettings.Get("De");
                }
                mmCorreo.From =new MailAddress( strMailFrom);
                mmCorreo.To.Add (new MailAddress(strMailTo));
                mmCorreo.Subject = strAsunto;
                mmCorreo.Body = strMensajeHTML;
                mmCorreo.BodyEncoding = Encoding.UTF8;
                //var smtp = new SmtpClient(ConfigurationManager.AppSettings.Get("SMTP"));
                //smtp.Send(mmCorreo);
                //NetworkCredential
                SmtpClient client  = new SmtpClient();
                client.Credentials = new System.Net.NetworkCredential("enovatraining.enovatraining.en@gmail.com", "gallospardos");
                client.Port = 587;
                client.Host = "smtp.gmail.com";
                client.EnableSsl = true; //Esto es para que vaya a través de SSL que es obligatorio con GMail
                try
                {
                    client.Send(mmCorreo);
                }
                catch (System.Net.Mail.SmtpException e7)
                {
                    throw new Exception(e7.Message);
                }
                booEstadoMail = true;

            }
            catch (Exception e8)
            {
                throw new Exception(e8.Message);
            }
            return booEstadoMail;
        }


    }
}
